package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;

public class JsonDateValueProcessorGenerator extends Generator{	
	private static final long serialVersionUID = -4790994605747142428L;
	protected Domain sampleDomain;
	protected boolean containsAuth = false;
	
	public Domain getSampleDomain() {
		return sampleDomain;
	}


	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

	public JsonDateValueProcessorGenerator(){
		super();		
		this.standardName = "JsonDateValueProcessorGenerator";
		this.fileName = "JsonDateValueProcessor.java";
	}	
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
    	List<Writeable> sList = new ArrayList<Writeable>();
        sList.add(new Statement(1000L,0,"package "+this.sampleDomain.getPackageToken()+".utils;"));
        sList.add(new Statement(2000L,0,""));
        sList.add(new Statement(3000L,0,"import java.text.SimpleDateFormat;"));
        sList.add(new Statement(4000L,0,"import java.util.Date;"));
        sList.add(new Statement(5000L,0,"import java.util.Locale;"));
        sList.add(new Statement(6000L,0,""));
        sList.add(new Statement(7000L,0,"import net.sf.json.JsonConfig;"));
        sList.add(new Statement(8000L,0,"import net.sf.json.processors.JsonValueProcessor;"));
        sList.add(new Statement(9000L,0,""));
        sList.add(new Statement(10000L,0,"public class JsonDateValueProcessor implements JsonValueProcessor {"));
        sList.add(new Statement(11000L,1,"public Object processArrayValue(Object value, JsonConfig config) {"));
        sList.add(new Statement(12000L,2,"return process(value);"));
        sList.add(new Statement(13000L,1,"}"));
        sList.add(new Statement(14000L,0,""));
        sList.add(new Statement(15000L,1,"public Object processObjectValue(String key, Object value, JsonConfig config) {"));
        sList.add(new Statement(16000L,2,"return process(value);"));
        sList.add(new Statement(17000L,1,"}"));
        sList.add(new Statement(18000L,1,"private Object process(Object value){"));
        sList.add(new Statement(19000L,2,"if (StringUtil.isBlank(value)) return \"\";"));
        
        sList.add(new Statement(19100L,2,"if (value instanceof java.sql.Date) {"));
        sList.add(new Statement(19200L,3,"Date tValue = new Date(((java.sql.Date)value).getTime());"));
        sList.add(new Statement(19300L,3,"if (tValue.getHours() != 0 || tValue.getMinutes() != 0 || tValue.getSeconds()!=0) {"));
        sList.add(new Statement(19400L,4,"return DateUtil.utilDateTimeToString(tValue);"));
        sList.add(new Statement(19500L,3,"} else {"));
        sList.add(new Statement(19600L,4,"return DateUtil.utilDateToString(tValue);"));
        sList.add(new Statement(19700L,3,"}"));
        sList.add(new Statement(19800L,2,"}"));
        
        sList.add(new Statement(20000L,2,"if(value instanceof Date){"));
        sList.add(new Statement(21000L,3,"Date tValue = (Date) value;"));
        sList.add(new Statement(22000L,3,"if (tValue.getHours() != 0 || tValue.getMinutes() != 0 || tValue.getSeconds()!=0) {"));
        sList.add(new Statement(23000L,4,"return DateUtil.utilDateTimeToString(tValue);"));
        sList.add(new Statement(24000L,3,"} else {"));
        sList.add(new Statement(25000L,4,"return DateUtil.utilDateToString(tValue);"));
        sList.add(new Statement(26000L,3,"}"));
        sList.add(new Statement(27000L,2,"}"));
        sList.add(new Statement(28000L,2,"return value.toString();"));
        sList.add(new Statement(29000L,1,"}"));
        sList.add(new Statement(30000L,0,"}"));        
        
    	return WriteableUtil.merge(sList);
	}
}
